package soket;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.Socket;

public class MyPushClient {
	private Socket socket;  
    public static void main(String[] args)  
    {  
        MyPushClient myPushClient = new MyPushClient();  
        try  
        {  
            myPushClient.start();  
        } catch (Exception e)  
        {  
            e.printStackTrace();  
        }  
    }  
    public void  start() throws Exception  
    {  
        socket = new Socket("127.0.0.1",3421);  
        System.out.println("server connection..");  
        ClientThread clientThread = new ClientThread();  
        clientThread.start();  
    }
    class ClientThread extends Thread{  
        private BufferedReader reader;  
        private BufferedWriter writer;  
  
        public ClientThread()  
        {  
  
            try  
            {  
                reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));  
                writer = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));  
            } catch (IOException e)  
            {  
                // TODO Auto-generated catch block  
                e.printStackTrace();  
            }  
        }  
        @Override  
        public void run()  
        {  
            String content = null;  
            while (true)  
            {  
                try  
                {  
                    if ((content = reader.readLine()) != null)  
                    {  
                        System.out.println(content);  
                        writer.write("收到消息");  
                        writer.newLine();  
                        writer.flush();  
                    }  
                } catch (IOException e) 
                {  
                    e.printStackTrace();
                    //断线重连
                    start();
                }  
            }  
        }  
    }  
}
